require 'set'

startTime = Time.now
puts "[INFO] job started at #{startTime}"
splitter = "\t"
freeprogramid = 'freeprogramid'
chargeprogramid = 'chargeprogramid'
top3userid = 'top3userid'
top10userid = 'top10userid'
viewlog30days = 'viewlog30days'
top3Freeviewlog30days = 'top3Freeviewlog30days'
top3Chargeviewlog30days = 'top3Chargeviewlog30days'
top10Freeviewlog30days = 'top10Freeviewlog30days'
top10Chargeviewlog30days = 'top10Chargeviewlog30days'
freeProgramIdSet = Set.new
chargeProgramIdSet = Set.new
top3UserIdSet = Set.new
top10UserIdSet = Set.new
begin
  # init free program set
  freeProgramIdFile = File.open(freeprogramid, 'r')
  freeProgramIdFile.each do |line|
    freeProgramIdSet.add(line.strip)
  end
  puts "[INFO] number of free program is #{freeProgramIdSet.size()}"
  # init charge program set
  chargeProgramIdFile = File.open(chargeprogramid, 'r')
  chargeProgramIdFile.each do |line|
    chargeProgramIdSet.add(line.strip)
  end
  puts "[INFO] number of charge program is #{chargeProgramIdSet.size()}"
  # init top 3 userid set
  top3UserIdFile = File.open(top3userid, 'r')
  top3UserIdFile.each do |line|
    top3UserIdSet.add(line.strip)
  end
  puts "[INFO] number of top 3 userid is #{top3UserIdSet.size()}"
  # init top 10 userid set
  top10UserIdFile = File.open(top10userid, 'r')
  top10UserIdFile.each do |line|
    top10UserIdSet.add(line.strip)
  end
  puts "[INFO] number of top 10 userid is #{top10UserIdSet.size()}"
  # seperate free and charge viewlog
  viewlog30daysFile = File.open(viewlog30days, 'r')
  top3Freeviewlog30daysFile = File.open(top3Freeviewlog30days, 'a')
  top3Chargeviewlog30daysFile = File.open(top3Chargeviewlog30days, 'a')
  top10Freeviewlog30daysFile = File.open(top10Freeviewlog30days, 'a')
  top10Chargeviewlog30daysFile = File.open(top10Chargeviewlog30days, 'a')
  i = 0
  t1 = Time.now
  viewlog30daysFile.each do |line|
    lineArr = line.split(splitter)
    if freeProgramIdSet.include?(lineArr[0].strip)
      if top3UserIdSet.include?(lineArr[1].strip)
        top3Freeviewlog30daysFile.write(line)
      elsif top10UserIdSet.include?(lineArr[1].strip)
        top10Freeviewlog30daysFile.write(line)
      end
    elsif chargeProgramIdSet.include?(lineArr[0].strip)
      if top3UserIdSet.include?(lineArr[1].strip)
        top3Chargeviewlog30daysFile.write(line)
      elsif top10UserIdSet.include?(lineArr[1].strip)
        top10Chargeviewlog30daysFile.write(line)
      end
    end
    i += 1
    if i % 100000 == 0
      t2 = Time.now
      puts "[DEBUG] #{i} lines have been processed......time consumption is #{t2-t1}"
      t1 = t2
    end
  end
ensure
  freeProgramIdFile.close
  chargeProgramIdFile.close
  viewlog30daysFile.close
  top3UserIdFile.close
  top10UserIdFile.close
  top3Freeviewlog30daysFile.close
  top3Chargeviewlog30daysFile.close
  top10Freeviewlog30daysFile.close
  top10Chargeviewlog30daysFile.close
end
endTime = Time.now
puts "[INFO] job has done at #{endTime}"
